package com.sojson.controller.po;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.controller.po.param.verify.OrderParamVerify;
import com.sojson.entity.bo.OrderParam;
import com.sojson.entity.po.Order;
import com.sojson.entity.vo.OrderVo;
import com.sojson.enums.EOrderType;
import com.sojson.result.Result;
import com.sojson.service.OrderService;
import com.sojson.util.StringUtil;
import com.sojson.util.pay.bean.PayResult;
import com.sojson.util.token.TokenUtil;

import io.swagger.annotations.Api;

/**
 * 订单表控制器
 * 
 * @author liu
 * @date 2021-03-16
 */
@Api(tags = "订单表管理")
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    @Resource
    private OrderService orderServiceImpl;

    /**
     * 单条插入
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Object insert(@RequestBody Order order) throws SQLException {
        orderServiceImpl.insert(order);
        return Result.success();
    }

    /**
     * 批量插入
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/insertList", method = RequestMethod.POST)
    public Object insertList(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.insertList(order));
    }

    /**
     * 通过对象批量删除(物理删除)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByDto", method = RequestMethod.POST)
    public Object deleteByDto(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.deleteByDto(order));
    }

    /**
     * 统计全部数量
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countAll", method = RequestMethod.GET)
    public Object countAll() throws SQLException {
        int countAll = orderServiceImpl.countAll();
        return Result.success(countAll);
    }

    /**
     * 查询所有
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findAll", method = RequestMethod.GET)
    public Object findAll() throws SQLException {
        List<Order> findAll = orderServiceImpl.findAll();
        return Result.success(findAll);
    }

    /**
     * 根据对象统计数量
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/countByDto", method = RequestMethod.POST)
    public Object countByDto(@RequestBody Order order) throws SQLException {
        int countByDto = orderServiceImpl.countByDto(order);
        return Result.success(countByDto);
    }

    /**
     * 根据对象查询
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByDto", method = RequestMethod.POST)
    public Object findByDto(@RequestBody Order order) throws SQLException {
        List<Order> findByDto = orderServiceImpl.findByDto(order);
        return Result.success(findByDto);
    }

    /**
     * 通过id删除(物理删除)
     * 
     * @param id
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @DeleteMapping(value = "/deleteById/{id}")
    @PreAuthorize("@ss.login()")
    public Object deleteById(@PathVariable Integer id) throws SQLException, IOException {
        orderServiceImpl.deleteById(id);
        return Result.success();
    }

    /**
     * 通过id列表删除(物理删除)
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/deleteByIdList", method = RequestMethod.POST)
    public Object deleteByIdList(@RequestParam("ids[]") List<Integer> ids) throws SQLException {
        return Result.success(orderServiceImpl.deleteByIdList(ids));
    }

    /**
     * 通过id更新(全部更新)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateById", method = RequestMethod.POST)
    public Object updateById(@RequestBody Order order) throws SQLException {
        orderServiceImpl.updateById(order);
        return Result.success();
    }

    /**
     * 通过id列表更新(全部更新)(更新内容相同)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticByIdList", method = RequestMethod.POST)
    public Object updateStaticByIdList(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.updateStaticByIdList(order));
    }

    /**
     * 通过id列表更新(全部更新)(更新内容不相同)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangeByIdList", method = RequestMethod.POST)
    public Object updateChangeByIdList(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.updateChangeByIdList(order));
    }

    /**
     * 通过id更新(局部更新)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updatePartialById", method = RequestMethod.POST)
    public Object updatePartialById(@RequestBody Order order) throws SQLException {
        orderServiceImpl.updatePartialById(order);
        return Result.success();
    }

    /**
     * 通过id列表更新(局部更新)(更新内容相同)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateStaticPartialByIdList", method = RequestMethod.POST)
    public Object updateStaticPartialByIdList(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.updateStaticPartialByIdList(order));
    }

    /**
     * 通过id列表更新(局部更新)(更新内容不相同)
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/updateChangePartialByIdList", method = RequestMethod.POST)
    public Object updateChangePartialByIdList(@RequestBody Order order) throws SQLException {
        return Result.success(orderServiceImpl.updateChangePartialByIdList(order));
    }

    /**
     * 通过id查询
     * 
     * @param id
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findById", method = RequestMethod.GET)
    public Object findById(Integer id) throws SQLException {
        Order findById = orderServiceImpl.findById(id);
        return Result.success(findById);
    }

    /**
     * 通过id列表查询
     * 
     * @param ids
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findByIdList", method = RequestMethod.GET)
    public Object findByIdList(@RequestParam("ids[]") List<Integer> ids) throws SQLException {
        List<Order> findByIdList = orderServiceImpl.findByIdList(ids);
        return Result.success(findByIdList);
    }

    /**
     * 通过对象查询id列表
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/findIdByDto", method = RequestMethod.POST)
    public Object findIdByDto(@RequestBody Order order) throws SQLException {
        List<Integer> findIdByDto = orderServiceImpl.findIdByDto(order);
        return Result.success(findIdByDto);
    }

    /**
     * 清空数据(物理清空)
     * 
     * @return
     * @throws SQLException
     */
    // @RequestMapping(value = "/clear", method = RequestMethod.POST)
    public Object clear() throws SQLException {
        orderServiceImpl.clear();
        return Result.success();
    }

    /**
     * 下单(购买)
     * 
     * @param orderParam
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/ordersBuy")
    @PreAuthorize("@ss.login()")
    public Object ordersBuy(@RequestBody OrderParam orderParam) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.ordersBuy(orderParam);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        orderParam.setOrderType(EOrderType.BUY.getCode());
        return Result.success(orderServiceImpl.ordersBuy(orderParam));
    }

    /**
     * 下单(新签)
     * 
     * @param orderParam
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/ordersNew")
    @PreAuthorize("@ss.login()")
    public Object ordersNew(@RequestBody OrderParam orderParam) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.ordersNew(orderParam);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        orderParam.setOrderType(EOrderType.NEW.getCode());
        return Result.success(orderServiceImpl.ordersNew(orderParam));
    }

    /**
     * 下单(续签)
     * 
     * @param goods
     * @return
     * @throws Exception 
     */
    @PostMapping(value = "/ordersGoOn")
    @PreAuthorize("@ss.login()")
    public Object ordersGoOn(@RequestBody Order order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.ordersGoOn(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        PayResult appOrders = orderServiceImpl.ordersGoOn(order);

        // InputStream inputStream = QrCodeUtils.getInputStream(appOrders.getCodeUrl());
        // FilterUtils.outFile(inputStream);

        return Result.success(appOrders);
    }

    /**
     * 获取订单(包含其他附加信息)
     * 
     * @param id
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/getInfo/{id}")
    @PreAuthorize("@ss.login()")
    public Object getInfo(@PathVariable Integer id) throws Exception {
        Order info = orderServiceImpl.getInfo(id);
        if (StringUtil.isNotBlankObject(info)) {
            info.setOrderEndTime(new Date(info.getCreatedAt().getTime() + 1000 * 60 * 30));
        }
        return Result.success(info);
    }

    /**
     * 根据对象查询
     * 
     * @param order
     * @return
     * @throws SQLException
     */
    @GetMapping(value = "/list")
    @PreAuthorize("@ss.hasPermi('/order/list')")
    public Object list(OrderVo order) throws SQLException {
        List<OrderVo> list = orderServiceImpl.list(order);
        return Result.success(list, list);
    }

    /**
     * 根据对象查询
     * 
     * @param order
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/listApp")
    @PreAuthorize("@ss.login()")
    public Object listApp(OrderVo order) throws SQLException, IOException {
        order.setUserNo(TokenUtil.getUserNo());
        List<OrderVo> list = orderServiceImpl.list(order);
        return Result.success(list, list);
    }

    /**
     * 根据对象查询
     * 
     * @param order
     * @return
     * @throws SQLException
     * @throws IOException 
     */
    @GetMapping(value = "/listMerchantApp")
    @PreAuthorize("@ss.login()")
    public Object listMerchantApp(OrderVo order) throws SQLException, IOException {
        order.setMerchantNo(TokenUtil.getUserNo());
        List<OrderVo> list = orderServiceImpl.list(order);
        return Result.success(list, list);
    }

    /**
     * 取消订单
     * 
     * @param no 订单编号
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/cancel/{no}")
    @PreAuthorize("@ss.login()")
    public Object cancel(@PathVariable String no) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.cancel(no);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(orderServiceImpl.cancel(no));
    }

    /**
     * 发货
     * 
     * @param order
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/sendOut")
    @PreAuthorize("@ss.hasPermi('/order/sendOut')")
    public Object sendOut(@RequestBody Order order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.sendOut(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Order ord = new Order();
        ord.updateInit();
        ord.setNo(order.getNo());
        ord.setExpressRentId(order.getExpressRentId());
        ord.setExpressRentCode(order.getExpressRentCode());
        ord.setExpressRentName(order.getExpressRentName());
        ord.setExpressRentShipperCode(order.getExpressRentShipperCode());
        ord.setExpressRentCustomerName(order.getExpressRentCustomerName());
        ord.setSendTime(ord.getUpdatedAt());
        return Result.success(orderServiceImpl.sendOut(ord));
    }

    /**
     * 归还
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/returnSendOut")
    @PreAuthorize("@ss.login()")
    public Object returnSendOut(@RequestBody Order order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.returnSendOut(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        Order ord = new Order();
        ord.updateInit();
        ord.setNo(order.getNo());
        ord.setExpressBackId(order.getExpressBackId());
        ord.setExpressBackCode(order.getExpressBackCode());
        ord.setExpressBackName(order.getExpressBackName());
        ord.setExpressBackShipperCode(order.getExpressBackShipperCode());
        ord.setExpressBackCustomerName(order.getExpressBackCustomerName());
        return Result.success(orderServiceImpl.returnSendOut(ord));
    }

    /**
     * 查询快递信息
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/findExpress/{orderId}")
    @PreAuthorize("@ss.login()")
    public Object findExpress(@PathVariable Integer orderId) throws Exception {
        return Result.success(orderServiceImpl.findExpress(orderId));
    }

    /**
     * 查询快递信息(归还信息)
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @GetMapping(value = "/findExpressBack/{orderId}")
    @PreAuthorize("@ss.login()")
    public Object findExpressBack(@PathVariable Integer orderId) throws Exception {
        return Result.success(orderServiceImpl.findExpressBack(orderId));
    }

    /**
     * 签收(购买)
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/signForBuy/{orderId}")
    @PreAuthorize("@ss.login()")
    public Object signForBuy(@PathVariable Integer orderId) throws Exception {
        return Result.success(orderServiceImpl.signForBuy(orderId));
    }

    /**
     * 签收(租赁)
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/signForNew/{orderId}")
    @PreAuthorize("@ss.login()")
    public Object signForNew(@PathVariable Integer orderId) throws Exception {
        return Result.success(orderServiceImpl.signForNew(orderId));
    }

    /**
     * 签收(归还)
     * 
     * @param orderId 订单ID
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/signForReturn")
    @PreAuthorize("@ss.hasPermi('/order/signForReturn')")
    public Object signForReturn(Integer orderId) throws Exception {
        return Result.success(orderServiceImpl.signForReturn(orderId));
    }

    /**
     * 退款
     * 
     * @param order 订单
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/refundNew")
    @PreAuthorize("@ss.hasPermi('/order/refundNew')")
    public Object refundNew(@RequestBody OrderVo order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.refundNew(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        return Result.success(orderServiceImpl.refundNew(order));
    }

    /**
     * 申请取消订单审核
     * 
     * @param order 订单
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/cancelAudit")
    @PreAuthorize("@ss.hasPermi('/order/cancelAudit')")
    public Object cancelAudit(@RequestBody OrderVo order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.cancelAudit(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        OrderVo o = new OrderVo();
        o.updateInit();
        o.setId(order.getId());
        o.setCancelAudit(order.getCancelAudit());
        return Result.success(orderServiceImpl.cancelAudit(o));
    }

    /**
     * 后台订单退款
     * 
     * @param order 订单
     * @return
     * @throws Exception 
     */
    @PutMapping(value = "/refund")
    @PreAuthorize("@ss.hasPermi('/order/refund')")
    public Object refund(@RequestBody OrderVo order) throws Exception {
        // 校验参数
        Result<?> paramResult = OrderParamVerify.refund(order);
        // 验证不通过
        if (!paramResult.getCode().equals(TransErrorCode.SUCCESS.getCode())) {
            return paramResult;
        }

        OrderVo o = new OrderVo();
        o.updateInit();
        o.setId(order.getId());
        o.setRefundTotal(order.getRefundTotal());
        return Result.success(orderServiceImpl.refund(o));
    }

}